package com.zqq.aidldemo.libServer;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
//import android.support.v4.os.IResultReceiver.Stub;

import com.zqq.aidldemo.MainActivity;
import com.zqq.aidldemo.R;

/**
 * @author zqq on 2018/7/17
 */
public class LibraryServer extends Service {

    private Stub stub;      //自定义Stub

    public LibraryServer() {
        this.stub = new Stub();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //添加下列代码将后台Service变成前台Service
        //构建“点击通知后打开MainActivity”的Intent对象
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        //新建Builder对象
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("前台服务通知的-标题");
        builder.setContentText("前台服务通知-内容");    //内容
        builder.setSmallIcon(android.R.mipmap.sym_def_app_icon);    //通知图标
        //设置点击通知后的操作
        builder.setContentIntent(pendingIntent);

        Notification notification = builder.getNotification();      //将Builder对象转变为普通的notification

        //LibraryServer以前台Service形式被开启
        startForeground(1, notification);   //让Service变成前台Service，并在系统中的状态栏显示出来
    }

    /**
     * 返回给客户端的是 Stub 类型，提示什么呢，下面方法会说明
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //返回给客户端
        return stub;
    }


    /**
     * 将Binder + BookCheckService 合成有跨进程提供服务的Stub
     * （Stub具有提供远程服务的能力）
     */
    class Stub extends Binder implements BookCheckService {

        @Override
        public String getBookInfo(String bookName) {
            return bookName + "的信息如下：***********";
        }

        @Override
        public String getBookList() {
            return "本馆所有书籍列表如下：\n" +
                    "1.*********\n" +
                    "2.************\n" +
                    "3.*********\n" +
                    "4.********88\n";
        }

        /**
         * stub重写Binder的方法，在分析Client代码时候提到Client调用 mRemote.transact会导致这个方法被调用
         */
        @Override
        protected boolean onTransact(int code, @NonNull Parcel data, @Nullable Parcel reply, int flags) throws RemoteException {
            switch (code) {
                //客户端发起"查询图书信息"请求则调用getBookInfo方法，并返回结果
                case IBinder.FIRST_CALL_TRANSACTION + 0:
                    data.enforceInterface("token10086");
                    //表示客户端传过来的bookName不为空
                    if (data.readInt() == 1) {
                        reply.writeNoException();

                        //调用上面方法（接口方法）
                        reply.writeString(getBookInfo(data.readString()));
                    } else {
                        reply.writeNoException();
                        reply.writeString("您输入的书名为空！");
                    }
                    return true;

                //客户端发起"获取图书列表"请求则调用getBookList方法，并返回结果
                case IBinder.FIRST_CALL_TRANSACTION + 1:
                    data.enforceInterface("token10000");
                    if (data.readInt() == 1) {
                        reply.writeNoException();

                        //调用上面方法（接口方法）
                        reply.writeString(getBookList());
                    } else {
                        reply.writeNoException();
                        reply.writeString("查询不到图书列表");
                    }
                    return true;
                default:
                    break;
            }
            return super.onTransact(code, data, reply, flags);
        }
    }


}
